﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using Microsoft.Win32;
    using PerpetuumSoft.InstrumentationPDA.Design;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;

    public abstract class Widget : Control
    {
        private PerpetuumSoft.InstrumentationPDA.Bevel bevel = new PerpetuumSoft.InstrumentationPDA.Bevel(BevelStyle.None, false, false);
        private Bitmap buffer;
        private IContainer components;
        private Bitmap staticLayer;
        private bool updateStatic = true;
        private static bool validated;

        protected Widget()
        {
            validated = true;
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            if (this.buffer != null)
            {
                this.buffer.Dispose();
                this.buffer = null;
            }
            if (this.staticLayer != null)
            {
                this.staticLayer.Dispose();
                this.staticLayer = null;
            }
            base.Dispose(disposing);
        }

        protected virtual void DrawBevel(Graphics g)
        {
            this.bevel.Draw(g, new Rectangle(0, 0, base.Size.Width - 1, base.Size.Height - 1));
        }

        protected virtual void DrawDynamic(Graphics g)
        {
        }

        protected virtual void DrawStatic(Graphics g)
        {
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected void InvalidateStatic()
        {
            this.updateStatic = true;
            if (this.staticLayer != null)
            {
                this.staticLayer.Dispose();
                this.staticLayer = null;
            }
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if ((!validated && (this.Site != null)) && this.Site.DesignMode)
            {
                bool flag = (this.Site != null) && this.Site.DesignMode;
                try
                {
                    string s = typeof(Widget).Assembly.GetName().Name.ToLower() + ".dll";
                    byte[] bytes = Encoding.ASCII.GetBytes(s);
                    byte[] buffer2 = new SHA1CryptoServiceProvider().ComputeHash(bytes);
                    byte[] buffer3 = (byte[]) Registry.CurrentUser.OpenSubKey(@"Software\Perpetuum Software").GetValue("Instrumentation Widgets for PDA (CF 2.0)");
                    if (buffer3.Length == 20)
                    {
                        int index = 0;
                        while (((index < buffer2.Length) && (index < buffer3.Length)) && (buffer2[index] == buffer3[index]))
                        {
                            index++;
                        }
                        if (index == 20)
                        {
                            flag = false;
                        }
                    }
                    
                }
                catch
                {
                }
                flag = false;
                if (flag)
                {
                    using (AboutForm form = new AboutForm())
                    {
                        form.ShowDialog();
                    }
                }
                validated = true;
            }
            base.OnPaint(pe);
            if (this.buffer == null)
            {
                this.buffer = new Bitmap(base.Width, base.Height);
            }
            if (this.staticLayer == null)
            {
                this.staticLayer = new Bitmap(base.Width, base.Height);
            }
            if (this.updateStatic)
            {
                Graphics graphics = Graphics.FromImage(this.staticLayer);
                graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, base.Width, base.Height);
                this.DrawStatic(graphics);
                this.DrawBevel(graphics);
                this.updateStatic = false;
            }
            Graphics g = Graphics.FromImage(this.buffer);
            g.DrawImage(this.staticLayer, 0, 0);
            this.DrawDynamic(g);
            pe.Graphics.DrawImage(this.buffer, 0, 0);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        protected override void OnResize(EventArgs e)
        {
            if (this.buffer != null)
            {
                this.buffer.Dispose();
                this.buffer = null;
            }
            if (this.staticLayer != null)
            {
                this.staticLayer.Dispose();
                this.staticLayer = null;
            }
            this.InvalidateStatic();
            base.OnResize(e);
        }

        protected int Round(decimal value)
        {
            return (int) Math.Round(value, 0);
        }

        public override System.Drawing.Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                if (base.BackColor != value)
                {
                    base.BackColor = value;
                    this.InvalidateStatic();
                }
            }
        }

        public PerpetuumSoft.InstrumentationPDA.Bevel Bevel
        {
            get
            {
                return this.bevel;
            }
            set
            {
                this.bevel = value;
                this.InvalidateStatic();
            }
        }

        protected virtual Size DefaultControlSize
        {
            get
            {
                return new Size(100, 100);
            }
        }

        protected virtual Rectangle WorkRectangle
        {
            get
            {
                Rectangle rectangle = new Rectangle(0, 0, base.Size.Width, base.Size.Height);
                int margin = this.bevel.GetMargin();
                rectangle.Inflate(-margin, -margin);
                return rectangle;
            }
        }
    }
}

